<template>
  <div class="info">
    <template>
      <el-skeleton :loading="loading"/>
    </template>
    <template v-if="!loading">
      <el-form :model="searchInfo" inline>
        <el-form-item>
          <el-input clearable v-model="searchInfo.name" placeholder="请输入商户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
          <el-button icon="el-icon-refresh-right" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <el-form inline>
        <el-form-item>
          <el-button type="success" @click="openWin()">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="infoList" border>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-form class="table-form" inline label-position="left" label-width="100px">
              <el-row>
                <el-col :span="8">
                  <el-form-item label="编号:">
                    <span>{{ scope.row.id }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="商户名:">
                    <span>{{ scope.row.name }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="地址:">
                    <span>{{ scope.row.address }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="介绍:">
                    <span>{{ scope.row.description }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="联系方式:">
                    <span>{{ scope.row.phone }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="宣传标语:">
                    <span>{{ scope.row.promotion_info }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="商户分类:">
                    <span>{{ scope.row.category }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="配送费:">
                    <span>{{ scope.row.float_delivery_fee }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="起送价格:">
                    <span>{{ scope.row.float_minimum_order_amount }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="开始营业时间:">
                    <span>{{ scope.row.opening_hours[0].split('/')[0] }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="结束营业时间:">
                    <span>{{ scope.row.opening_hours[0].split('/')[1] }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="商户门面:">
                    <el-image style="height: 100px;width: 100px" fit="fill"
                              :src="'http://192.168.26.105:8082/file/getImgStream?fileName='+scope.row.image_path"></el-image>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="商户名" prop="name"/>
        <el-table-column label="联系方式" prop="phone"/>
        <el-table-column label="口号" prop="promotion_info" show-overflow-tooltip/>
        <el-table-column label="商户分类" prop="category"/>
        <el-table-column label="是否启用" prop="disabled">
          <template slot-scope="scope">
            <el-button type="success" v-if="scope.row.disabled===0" size="small">启用</el-button>
            <el-button type="danger" v-else size="small" title="点击切换启用" @click="updateDisabled(scope.row)">禁用
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="食物操作" width="200px">
          <template slot-scope="scope">
            <el-button type="success" size="small"
                       @click="$router.push({name:'foodEdit',params:{restaurant_id:scope.row.id}})">添加食物
            </el-button>
            <el-button type="primary" size="small" @click="showFood(scope.row.id)">查看食物</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="openWin(scope.row)">修改</el-button>
            <el-button type="danger" size="small" @click="delInfo(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="total,prev,pager,next,sizes,jumper"
                     :total="pageInfo.total"
                     :page-size="pageInfo.size"
                     :current-page="pageInfo.current"
                     :page-sizes="[5,10,20,30,50,100]"
                     @size-change="sizeChange"
                     @current-change="currentChange"
      />
    </template>
    <el-dialog title="食物列表" :visible.sync="foodVisible">
      <Food ref="foodList"/>
    </el-dialog>
  </div>
</template>

<script>
import Food from '@/views/shop/food/Food'

export default {
  name: 'Shop',
  data() {
    return {
      loading: true,
      searchInfo: {
        name: '',
        page: 1,
        limit: 5
      },
      infoList: [],
      pageInfo: {},
      foodVisible: false
    }
  },
  components: {Food},
  methods: {
    getList() {
      this.$http.get('shopping/listShop', this.searchInfo).then(({data}) => {
        this.loading = false
        if (data.code === 20000) {
          this.infoList = data.data.records
          this.pageInfo = data.data
        }
      })
    },
    sizeChange(limit) {
      this.searchInfo.limit = limit
      this.getList()
    },
    currentChange(page) {
      this.searchInfo.page = page
      this.getList()
    },
    openWin(info) {
      if (info) {
        this.$router.push('shopEdit?id=' + info.id)
      } else {
        this.$router.push('shopEdit')
      }
    },
    delInfo(id) {
      this.$confirm('确定删除该数据吗？', '询问').then(() => {
        this.$axios.delete('shopping/restaurants/' + id).then(({data}) => {
          if (data.code === 20000) {
            this.$message({
              message: '删除成功',
              type: 'success',
              onClose: () => {
                this.getList()
              }
            })
          } else {
            this.$alert('删除失败！', {type: 'error'})
          }
        })
      })
    },
    resetForm() {
      this.searchInfo.account = ''
      this.searchInfo.name = ''
      this.getList()
    },
    showFood(shopId) {
      this.foodVisible = true
      this.$nextTick(() => {
        this.$refs.foodList.init(shopId)
      })
    },
    updateDisabled(shop) {
      const disabled = shop.disabled === 0 ? 1 : 0
      const id = shop.id
      const msg=shop.disabled===0?"确定禁用商铺吗？":"确定启用商铺吗?"
      this.$confirm(msg).then(()=>{
        this.$http.post("/shopping/stopShop",{id,disabled}).then(({data})=>{
          if (data.code===20000){
            this.$message({
              message:"更新状态成功",
              type:"success",
              onClose:()=>{
                this.getList()
              }
            })
          }
        })
      })
    }
  },
  created() {
    this.$nextTick(() => {
      this.getList()
    })
  }
}
</script>

<style scoped lang="scss">

</style>
